package com.dy.service.impl;

import com.dy.domain.EchartsTu;
import com.dy.domain.FenLei;
import com.dy.domain.TuShu;
import com.dy.domain.XinXi;
import com.dy.domain.pojo.PageBean;
import com.dy.mapper.TuShuMapper;
import com.dy.service.TuShuService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class TuSHuServiceImpl implements TuShuService {

    @Autowired
    private TuShuMapper tuShuMapper;

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public PageBean<TuShu> findPage(Integer pageNo, Integer pageSize, String ming) {

        PageHelper.startPage(pageNo,pageSize);

        List<TuShu> tushu = tuShuMapper.mohu(ming);

        PageInfo<TuShu> info = new PageInfo<>(tushu);

        PageBean<TuShu> pageBean = new PageBean<>();

        pageBean.setTotalCount(info.getTotal());
        pageBean.setPageCount(info.getPages());
        pageBean.setObject(info.getList());

        return pageBean;
    }

    @Override
    public void add(TuShu tuShu){
        tuShuMapper.add(tuShu);
    }

    @Override
    public TuShu huixian(Integer tid) {
        return tuShuMapper.huixian(tid);
    }

    @Override
    public void update(TuShu tuShu) {
        tuShuMapper.update(tuShu);
    }

    @Override
    public void del(Integer tid) {
        tuShuMapper.del(tid);
    }

    @Override
    public EchartsTu cha() {

        List<XinXi> list = tuShuMapper.cha();

        ArrayList<Integer> value = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();

        for (XinXi xinXi : list) {
            value.add(xinXi.getValue());
            name.add(xinXi.getName());
        }
        return new EchartsTu(name,value);
    }

    @Override
    public List<TuShu> findall() {
        return tuShuMapper.findall();
    }
}
